<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="销售订单ID" prop="soId">
        <el-input
          v-model="queryParams.soId"
          placeholder="请输入销售订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售订单编号" prop="soNumber">
        <el-input
          v-model="queryParams.soNumber"
          placeholder="请输入销售订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售订单名称" prop="soName">
        <el-input
          v-model="queryParams.soName"
          placeholder="请输入销售订单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户ID" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="clientCode">
        <el-input
          v-model="queryParams.clientCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单日期" prop="orderDate">
        <el-date-picker clearable
          v-model="queryParams.orderDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择订单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预计交货日期" prop="deliveryDate">
        <el-date-picker clearable
          v-model="queryParams.deliveryDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预计交货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="销售人员" prop="salesPerson">
        <el-input
          v-model="queryParams.salesPerson"
          placeholder="请输入销售人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态(草稿，已审批，已完成，已退货)" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态(草稿，已审批，已完成，已退货)" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="销售部门id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入销售部门id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入销售部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="喷漆要求" prop="paintingRequirements">
        <el-input
          v-model="queryParams.paintingRequirements"
          placeholder="请输入喷漆要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备方向" prop="deviceOrientation">
        <el-input
          v-model="queryParams.deviceOrientation"
          placeholder="请输入设备方向"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xs:changeRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['xs:changeRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xs:changeRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xs:changeRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="changeRecordList" border @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="变更记录ID" align="center" prop="recordId" />
      <el-table-column label="销售订单ID" align="center" prop="soId" />-->
      <el-table-column label="销售订单编号" align="center" prop="soNumber" :show-overflow-tooltip="true"/>
      <el-table-column label="销售订单" align="center" prop="soName" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="客户ID" align="center" prop="clientId" />-->
      <el-table-column label="客户编码" align="center" prop="clientCode" />
      <el-table-column label="客户名称" align="center" prop="clientName" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="总金额" align="center" prop="totalAmount" />-->
      <el-table-column label="订单日期" align="center" prop="orderDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预交付日期" align="center" prop="deliveryDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售人员" align="center" prop="salesPerson" />
<!--      <el-table-column label="状态(草稿，已审批，已完成，已退货)" align="center" prop="status" />-->
<!--      <el-table-column label="销售部门id" align="center" prop="deptId" />-->
      <el-table-column label="销售部门" align="center" prop="deptName" />
      <el-table-column label="喷漆要求" align="center" prop="paintingRequirements" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="设备方向" align="center" prop="deviceOrientation" />
      <el-table-column label="说明" align="center" prop="shuoming" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="文件url" align="center" prop="fileUrl" />-->
      <el-table-column label="备注" align="center" prop="remarks" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['xs:changeRecord:query']"
          >查看</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xs:changeRecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xs:changeRecord:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售订单变更记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="销售订单编号" prop="soNumber">
              <el-input v-model="form.soNumber" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售订单名称" prop="soName">
              <el-input v-model="form.soName" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户编码" prop="clientCode">
              <el-input v-model="form.clientCode" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单日期" prop="orderDate">
              <el-date-picker
                  clearable
                  v-model="form.orderDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计交货日期" prop="deliveryDate">
              <el-date-picker
                  clearable
                  v-model="form.deliveryDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="销售人员" prop="salesPerson">
              <el-input v-model="form.salesPerson" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="form.deptName" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单状态">
              <el-input v-model="form.status" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="喷漆要求" prop="paintingRequirements">
              <el-input v-model="form.paintingRequirements" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="deviceCode">
              <el-input v-model="form.deviceCode" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备方向" prop="deviceOrientation">
              <el-input v-model="form.deviceOrientation" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="说明" prop="shuoming">
              <el-input v-model="form.shuoming" type="textarea" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <!--        <el-form-item label="销售订单ID" prop="soId">
          <el-input v-model="form.soId" placeholder="请输入销售订单ID" />
        </el-form-item>-->
<!--        <el-form-item label="客户ID" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户ID" />
        </el-form-item>-->
<!--        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>-->
<!--        <el-form-item label="销售部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入销售部门id" />
        </el-form-item>-->
        <el-form-item label="合同" prop="fileUrl">
          <FileUpload
              :limit="1"
              :fileSize="100"
              :fileType="fileType"
              :value="fileList"
              :optType="optType"
              @onUploaded="handleUploaded"
              @onRemoved="handleRemoved">
          </FileUpload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChangeRecord,
  getChangeRecord,
  delChangeRecord,
  addChangeRecord,
  updateChangeRecord
} from "@/api/mes/xs/changeRecord";
import FileUpload from "@/components/FileUpload/index";

export default {
  name: "ChangeRecord",
  components: {
    FileUpload
  },
  props: {
    soId: null,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单变更记录表格数据
      changeRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soId: this.soId,
        soNumber: null,
        soName: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        totalAmount: null,
        orderDate: null,
        deliveryDate: null,
        salesPerson: null,
        status: null,
        deptId: null,
        deptName: null,
        paintingRequirements: null,
        deviceCode: null,
        deviceOrientation: null,
        shuoming: null,
        fileUrl: null,
        remarks: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 上传文件类型
      fileType: ["pdf", "doc", 'docx', "xls", 'xlsx', "jpg", "png"],
      // 上传文件列表
      fileList: [],
      // 查看开关
      optType: 'view',
    };
  },
  watch: {
    soId: function (newVal, oldVal) {
      if (newVal !== oldVal) {
        this.queryParams.soId = newVal;
        this.getList();
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售订单变更记录列表 */
    getList() {
      this.loading = true;
      listChangeRecord(this.queryParams).then(response => {
        this.changeRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        soId: null,
        soNumber: null,
        soName: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        totalAmount: null,
        orderDate: null,
        deliveryDate: null,
        salesPerson: null,
        status: "0",
        deptId: null,
        deptName: null,
        paintingRequirements: null,
        deviceCode: null,
        deviceOrientation: null,
        shuoming: null,
        fileUrl: null,
        remarks: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        instanceId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售订单变更记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getChangeRecord(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售订单变更记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateChangeRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChangeRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除销售订单变更记录编号为"' + recordIds + '"的数据项？').then(function() {
        return delChangeRecord(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xs/changeRecord/export', {
        ...this.queryParams
      }, `changeRecord_${new Date().getTime()}.xlsx`)
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getChangeRecord(recordId).then(response => {
        this.form = response.data;
        response.data.fileUrl === null ? this.fileList = [] : this.fileList = response.data.fileUrl.split(',');
        this.open = true;
        this.title = "销售订单变更记录详情";
      });
    },
    // 上传成功回调
    handleUploaded(data) {
      this.form.fileUrl = data;
    },
    // 删除成功回调
    handleRemoved(data) {
      this.form.fileUrl = data;
    },
  }
};
</script>
